package com.decent.text.pdf.test2;


import java.io.*;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;

import static java.lang.System.in;

/**
 * @author 王雪杰
 * @date 2021/11/12 17:48
 */
public class CreateHtmlByFreemarker {


    public static void main(String[] args) throws IOException {
        String str = getHtmlStr();
        System.out.println(str);
        // FileOutputStream out = null;
        String htmlStr = CreateHtmlByFreemarker.getHtmlStr();
        try {
            SimpleDateFormat simp = new SimpleDateFormat("HH-mm-ss");
            String times = simp.format(new Date()).trim();
            String root = "D:\\idea\\workspace\\test-4-7\\src\\main\\resources\\pdfTest" + File.separator;
            File deskFile = new File(root + times + ".html");
            // out = new FileOutputStream(root + times + ".html");
            // String str="hello world!";
            OutputStreamWriter writer;
            try {
                writer = new OutputStreamWriter(new FileOutputStream(root + times + ".html"), StandardCharsets.UTF_8);
                writer.write(htmlStr);
                writer.flush();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }


            // out.flush();


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            in.close();
            // out.close();
        }
    }

    public static String getHtmlStr() {
        // step1 创建freeMarker配置实例
        Configuration configuration = new Configuration();
        StringWriter stringWriter = new StringWriter();
        BufferedWriter writer = new BufferedWriter(stringWriter);
        try {
            // step2 获取模版路径
            String templatePath = "D:\\idea\\workspace\\test-4-7\\src\\main\\resources\\pdfTest";
            // templatePath = java.net.URLDecoder.decode(templatePath, "utf-8");//这里我的路径有空格添加此处理
            configuration.setDirectoryForTemplateLoading(new File(templatePath));
            configuration.setDefaultEncoding("UTF-8");
            // StringBuilder sb = new StringBuilder();
            // sb.append("<div>");
            // sb.append("<img style='height:100px;width:200px;display:block;' src='https://dss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2534506313,1688529724&fm=26&gp=0.jpg' />");
            // sb.append("</br><span>wesley 演示 导出富文本！@@#######￥￥%%%%………………&&&**~~~~~~&&&&&&&&、、、、、、、、</span>");
            // sb.append("</br><span>----多图分割线---</span>");
            // sb.append("<img style='height:100px;width:200px;display:block;' src='https://dss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2534506313,1688529724&fm=26&gp=0.jpg' />");
            // sb.append("</br><span>中国梦，幸福梦！</span>");
            // sb.append("</div>");
            // sb.append("<table style='border: 0.5px solid #000' border='0' cellspacing='0' cellpadding='0'>");
            // sb.append("<tr>");
            // sb.append(" <th style='border: 0.5px solid #000'>Month</th>");
            // sb.append("<th style='border: 0.5px solid #000'>Savings</th>");
            // sb.append("</tr>");
            // sb.append("<tr>");
            // sb.append("<td style='border: 0.5px solid #000'>January</td>");
            // sb.append("<td style='border: 0.5px solid #000'>$100</td>");
            // sb.append("</tr>");
            // sb.append("</table>");

            // step3 创建数据模型
            Map<String, Object> dataMap = new HashMap(2);
            dataMap.put("name", "wesley");
            // dataMap.put("datetime", "2017-05-10");
            // dataMap.put("title", "演示demo");
            // dataMap.put("context1", sb.toString());
            // dataMap.put("context2", sb.toString());
            // dataMap.put("context3", sb.toString());
            // dataMap.put("context4", sb.toString());
            // dataMap.put("context5", sb.toString());
            // dataMap.put("context6", sb.toString());
            // step4 加载模版文件
            Template template = configuration.getTemplate("new测试pdf.ftl");
            // step5 生成数据
            template.process(dataMap, writer);
            String htmlStr = stringWriter.toString();
            System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^user.ftl 文件创建成功 !");
            return htmlStr;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}